package com.hdrx.fundhelper.utils;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: houdx
 * @description: 发短信
 * @date: 2023/11/27 14:36
 */
public class SmsUtil {
    public static void main(String[] args) {
       /* //短信api调用地址
        String host = "https://dfsns.market.alicloudapi.com";
        //请求路径
        String path = "/data/send_sms";
        //请求方式
        String method = "POST";
        //appcode：自己的AppCode，可以在自己的控制台找到，是一串32位的字符串
        String appcode = "b2ffea514dc74274a26f669899176348";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE b2ffea514dc74274a26f669899176348
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();

        //使用随机数生成4位验证码
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int randomNum = (int) (Math.random() * 10);
            stringBuilder.append(randomNum);
        }
        String code = stringBuilder.toString();
        //这里是要发送给用户的验证码，我们使用了随机数进行生成
        bodys.put("content", "code:" + code);
        //这里是接受短信的手机号码，我们在实际开发时这里就不能写死了
        bodys.put("phone_number", "18503876051");
        //这里是发送短信的模板ID：代表整体短信文案模板
        bodys.put("template_id", "43fb6ea5f1e6419089f21496a4ee184c");
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

        String host = "https://gyytz.market.alicloudapi.com";
        String path = "/sms/smsSend";
        String method = "POST";
        String appcode = "b2ffea514dc74274a26f669899176348";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", "18503876051");
        //querys.put("param", "**code**:12345,**minute**:5");

//smsSignId（短信前缀）和templateId（短信模板），可登录国阳云控制台自助申请。参考文档：http://help.guoyangyun.com/Problem/Qm.html

        //querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2");
        querys.put("templateId", "43fb6ea5f1e6419089f21496a4ee184c");
        Map<String, String> bodys = new HashMap<String, String>();


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从\r\n\t    \t* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java\r\n\t    \t* 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
